import QtQuick 2.7
import QtQuick.Controls 2.12
import QtQuick.Window 2.2

Rectangle {
    property bool isMaximized: false;
    property alias showTxt: wndTitle.text

    height: 50;
    color: "#393636"

    MouseArea {
        property bool isDoubleClicked:false
        property point clickPos: "0,0"

        anchors.fill: parent
        //只处理鼠标左键
        acceptedButtons: Qt.LeftButton
        // 按下事件
        onPressed: {
            isDoubleClicked = false;
            clickPos = Qt.point(mouse.x,mouse.y)
        }
        // 位置移动事件
        onPositionChanged: {
            // 在非全屏的情况下移动窗口
            if(!isDoubleClicked && pressed && rootWnd.visibility !== Window.Maximized && rootWnd.visibility !== Window.FullScreen) {
                var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
                rootWnd.x += delta.x
                rootWnd.y += delta.y
            }
            // 在全屏的情况下移动窗口
            if(rootWnd.visibility === Window.Maximized && pressed && !isDoubleClicked) {
                isMaximized = false;
                rootWnd.showNormal();
            }
        }
        // 双击事件
        onDoubleClicked: {
            isDoubleClicked = true;
            if(isMaximized){
                isMaximized = false;
                rootWnd.showNormal();
            }else{
                isMaximized = true;
                rootWnd.showMaximized();
            }
        }
    }

    Text {
        id: wndTitle
        width: parent.width
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: parent.left
        anchors.leftMargin: 30
        font {
            family: "Microsoft YaHei"
            pixelSize: 20
        }
        color: "white"
        text: "Window Title"
    }
    Button {
        id:closeBtn
        width: 20
        height:20
        anchors {
            right: parent.right
            rightMargin: 30
            verticalCenter: parent.verticalCenter
        }
        hoverEnabled: true
        background: Rectangle {
            color: Qt.rgba(0,0,0,0)
            Image {
                anchors.fill: parent;
                source: "qrc:/images/close.png"
            }
        }
        onClicked: {
            rootWnd.close()
        }
    }
    Button {
        id: normBtn;
        width: 20
        height:20
        anchors {
            right: closeBtn.left
            rightMargin: 10
            verticalCenter: parent.verticalCenter
        }
        hoverEnabled : true
        background: Rectangle {
            color: Qt.rgba(0,0,0,0);
            Image {
                id:normBtnBg;
                anchors.fill: parent;
                source: "qrc:/images/max.png"
            }
        }
        onClicked:{
            if(isMaximized){
                isMaximized = false;
                rootWnd.showNormal();
            }else{
                isMaximized = true;
                rootWnd.showMaximized();
            }
        }
    }
    Button {
        id: minBtn
        width: 20
        height:20
        anchors{
            right: normBtn.left
            rightMargin: 10
            verticalCenter: parent.verticalCenter
        }
        hoverEnabled: true
        background: Rectangle {
            color: Qt.rgba(0,0,0,0);
            Image{
                anchors.fill: parent;
                source: "qrc:/images/min.png"
            }
        }
        onClicked:{
            rootWnd.showMinimized();
        }
    }
}
